
import wx
import os
import images
import public
import suffix
import os


class mTreeCtrl(wx.TreeCtrl):
    def __init__(self, parent, *a, **kw):
        wx.TreeCtrl.__init__(self, parent, *a, **kw)
        isz = (16,16)
        il = wx.ImageList(isz[0], isz[1])
        self.fldridx     = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER,      wx.ART_OTHER, isz))
        self.fldropenidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER_OPEN, wx.ART_OTHER, isz))
        self.fileidx     = il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz))
        self.smileidx    = il.Add(images.Smiles.GetBitmap())
        self.SetImageList(il)
        self.il = il
        root_path = os.getcwd()
        self.root = self.AddRoot(os.path.basename(root_path))
        self.SetItemData(self.root, None)
        self.SetItemImage(self.root, self.fldridx, wx.TreeItemIcon_Normal)
        self.SetItemImage(self.root, self.fldropenidx, wx.TreeItemIcon_Expanded)
        self.Expand(self.root)
        rmenu = wx.Menu()
        public.myMenu(self, rmenu, ("重命名", "复制", "移动", "粘贴", "删除"))
        self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, lambda eve, menu=rmenu: self.on_ShowPopup(eve, menu))
        self.Bind(wx.EVT_LEFT_DCLICK, self.on_click)
        #wx.CallAfter(lambda : self.gettree(root_path, self.root))

    def on_ShowPopup(self, eve, menu):
        self.PopupMenu(menu)

    def on_click(self, eve):
        f = self.HitTest(eve.GetPosition())
        r = self.GetItemData(f[0])
        if r and not os.path.isdir(r):
            obj, _type = suffix.ux_suffix(r)
            name = os.path.basename(r)
            windows = self.GetParent().GetParent().book
            #windows = eve.GetEventObject().GetParent().GetParent().book
            windows.mAddPage(obj(windows, r, _type), name)
        eve.Skip()

    def on_menu(self, eve, value):
    	print(eve,value)

    def gettree(self, path, dirp):
        trees = os.listdir(path)
        for v in trees:
            path_1 = os.path.join(path, v)
            if os.path.isdir(path_1):
                child = self.AppendItem(dirp, v)
                self.SetItemData(child, path_1)
                self.SetItemImage(child, self.fldridx, wx.TreeItemIcon_Normal)
                self.SetItemImage(child, self.fldropenidx, wx.TreeItemIcon_Expanded)
                self.gettree(path_1, child)
            else:
                item = self.AppendItem(dirp,  v)
                self.SetItemData(item, path_1)
                self.SetItemImage(item, self.fileidx, wx.TreeItemIcon_Normal)
                self.SetItemImage(item, self.smileidx, wx.TreeItemIcon_Selected)
        self.Layout()

    def mupdate(self, path):
        self.DeleteAllItems()
        self.root = self.AddRoot(os.path.basename(path))
        self.SetItemData(self.root, None)
        self.SetItemImage(self.root, self.fldridx, wx.TreeItemIcon_Normal)
        self.SetItemImage(self.root, self.fldropenidx, wx.TreeItemIcon_Expanded)
        self.Expand(self.root)
        wx.CallAfter(lambda : self.gettree(path, self.root))